package com.ruoyi.web.admin.data;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

/**
 * 设备消息控制器
 *
 * @author 10100
 */
@Api(tags = "设备消息管理")
@RestController
@RequestMapping("/device/message")
public class DeviceMessageController {

    @ApiOperation("发送消息到指定设备")
    @PostMapping("/send")
    @Anonymous
    public AjaxResult sendMessage(
            @ApiParam("设备标识") @RequestParam String deviceIdentifier,
            @ApiParam("消息内容") @RequestParam String message) {
        boolean success = DeviceSocketManager.sendToDevice(deviceIdentifier, message);
        return success ? AjaxResult.success("消息发送成功") : AjaxResult.error("设备未连接或消息发送失败");
    }

    @ApiOperation("广播消息到所有设备")
    @PostMapping("/broadcast")
    @Anonymous
    public AjaxResult broadcastMessage(@ApiParam("消息内容") @RequestParam String message) {
        DeviceSocketManager.broadcastMessage(message);
        return AjaxResult.success("广播消息发送成功");
    }

    @ApiOperation("检查设备是否在线")
    @GetMapping("/check")
    @Anonymous
    public AjaxResult checkDeviceOnline(@ApiParam("设备标识") @RequestParam String deviceIdentifier) {
        boolean isOnline = DeviceSocketManager.isDeviceOnline(deviceIdentifier);
        return AjaxResult.success("设备状态查询成功", isOnline);
    }

    /**
     * 获取所有建立连接的设备
     */
    @ApiOperation(value = "获取所有建立连接的设备", notes = "获取所有建立连接的设备")
    @GetMapping("/list")
    @Anonymous
    public AjaxResult clients() {
        return AjaxResult.success(DeviceSocketManager.getAllClientsKey());
    }

    /**
     * 发送服务信息设置消息
     */
    @ApiOperation(value = "发送服务信息设置消息", notes = "发送服务信息设置消息")
    @GetMapping("/sendSetServerMessage")
    @Anonymous
    public AjaxResult sendSetServerMessage(String deviceId, String responseBody) {
        DeviceChannelInfo deviceChannelInfo = DeviceSocketManager.getClient(deviceId);
        String iccid = deviceChannelInfo.getIccid();
        String message = CommandMessageBuilder.buildSetServerMessage(deviceId, iccid, responseBody);
        boolean success = DeviceSocketManager.sendToDevice(deviceId, message);
        return success ? AjaxResult.success("消息发送成功") : AjaxResult.error("设备未连接或消息发送失败");
    }

    /**
     * 发送获取实时位置消息
     */
    @ApiOperation(value = "发送获取实时位置消息", notes = "发送获取实时位置消息")
    @GetMapping("/sendGetLocationMessage")
    @Anonymous
    public AjaxResult sendGetLocationMessage(String deviceId) {
        DeviceChannelInfo deviceChannelInfo = DeviceSocketManager.getClient(deviceId);
        String iccid = deviceChannelInfo.getIccid();
        String message = CommandMessageBuilder.buildGetLocationMessage(deviceId, iccid);
        boolean success = DeviceSocketManager.sendToDevice(deviceId, message);
        return success ? AjaxResult.success("消息发送成功") : AjaxResult.error("设备未连接或消息发送失败");
    }

    /**
     * 发送设置定位频率消息
     */
    @ApiOperation(value = "发送设置定位频率消息", notes = "发送设置定位频率消息")
    @GetMapping("/sendLocationSeqSetMessage")
    @Anonymous
    public AjaxResult sendLocationSeqSetMessage(String deviceId, String seq) {
        DeviceChannelInfo deviceChannelInfo = DeviceSocketManager.getClient(deviceId);
        String iccid = deviceChannelInfo.getIccid();
        String message = CommandMessageBuilder.buildLocationSeqSetMessage(deviceId, iccid, seq);
        boolean success = DeviceSocketManager.sendToDevice(deviceId, message);
        return success ? AjaxResult.success("消息发送成功") : AjaxResult.error("设备未连接或消息发送失败");
    }

    /**
     * 发送设置定位频率消息
     */
    @ApiOperation(value = "发送设置定位频率消息ZF1036", notes = "发送设置定位频率消息ZF1036")
    @GetMapping("/buildLocationSeq2SetMessage")
    @Anonymous
    public AjaxResult sendLocationSeq2SetMessage(String deviceId, String seq) {
        DeviceChannelInfo deviceChannelInfo = DeviceSocketManager.getClient(deviceId);
        String iccid = deviceChannelInfo.getIccid();
        String message = CommandMessageBuilder.buildLocationSeq2SetMessage(deviceId, iccid, seq);
        boolean success = DeviceSocketManager.sendToDevice(deviceId, message);
        return success ? AjaxResult.success("消息发送成功") : AjaxResult.error("设备未连接或消息发送失败");
    }

    /**
     * 设置健康数据上报频率
     */
    @ApiOperation(value = "设置健康数据上报频率", notes = "设置健康数据上报频率")
    @GetMapping("/sendHealthIntervalMessage")
    @Anonymous
    public AjaxResult sendHealthIntervalMessage(String deviceId, String interval) {
        DeviceChannelInfo deviceChannelInfo = DeviceSocketManager.getClient(deviceId);
        String iccid = deviceChannelInfo.getIccid();
        String message = CommandMessageBuilder.buildHealthIntervalMessage(deviceId, iccid, interval);
        boolean success = DeviceSocketManager.sendToDevice(deviceId, message);
        return success ? AjaxResult.success("消息发送成功") : AjaxResult.error("设备未连接或消息发送失败");
    }
}